package com.yue.ssa.controller;

import com.yue.ssa.bean.RespBean;
import com.yue.ssa.bean.Role;
import com.yue.ssa.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhengyl
 * @description
 * @date 2020-07-31 14:28
 */
@Api(value = "角色管理", tags = "角色管理接口")
@RestController
@RequestMapping("rest/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PreAuthorize("hasAuthority('system_role::read')")
    @ApiOperation(value = "根据角色id获取角色信息", notes = "根据角色id获取角色信息")
    @ApiImplicitParam(name = "id", value = "角色主键", required = true, dataType = "String")
    @GetMapping("/{id}")
    public RespBean get(@PathVariable("id") String id) {
        return roleService.get(id);
    }

    @ApiOperation(value = "判断角色名称是否存在", notes = "判断角色名称是否存在")
    @ApiImplicitParam(name = "name", value = "角色名称", required = true, dataType = "String")
    @GetMapping("/nameIsExist")
    public boolean nameIsExist(String name) {
        return roleService.nameIsExist(name);
    }

    @PreAuthorize("hasAuthority('system_role::write')")
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @PostMapping
    public RespBean add(@RequestBody Role role) {
        return roleService.add(role);
    }

    @PreAuthorize("hasAuthority('system_role::write')")
    @ApiOperation(value = "修改角色", notes = "修改角色")
    @PutMapping
    public RespBean update(@RequestBody Role role) {
        return roleService.update(role);
    }

    @PreAuthorize("hasAuthority('system_role::read')")
    @ApiOperation(value = "角色信息分页", notes = "角色信息分页")
    @ApiImplicitParams({@ApiImplicitParam(value = "关键字", name = "keyword", paramType = "query"),
            @ApiImplicitParam(value = "页码", name = "pageNo", paramType = "query"),
            @ApiImplicitParam(value = "条数", name = "pageSize", paramType = "query")})
    @GetMapping("page")
    public RespBean page(String keyword, @RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize) {
        return roleService.page(keyword, pageNo, pageSize);
    }

    @PreAuthorize("hasAuthority('system_role::write')")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParam(name = "ids", value = "角色主键", required = true, allowMultiple = true, dataType = "String")
    @DeleteMapping("/delete")
    public RespBean delete(@RequestParam String[] ids) {
        return roleService.delete(ids);
    }
}
